package com.aiyige.page.player;

import android.annotation.SuppressLint;
import android.app.Service;
import android.media.AudioManager;
import android.os.Build;
import android.support.annotation.RequiresApi;

import com.aiyige.MyApp;
import com.aiyige.base.BaseFragment;
import com.aiyige.base.eventbus.EventNetworkChanged;

import org.greenrobot.eventbus.Subscribe;

/**
 * Created by xiachao on 2017/11/14.
 */

public class PlayerFragment extends BaseFragment{

    long lastPlayTime;

    public Integer volume = null;

    public PlayerFragment() {

    }

    private AudioManager mAudioManager;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @SuppressLint("ValidFragment")
    public PlayerFragment(PlayerView playerView) {
        this.mPlayerView = playerView;
        mAudioManager = (AudioManager) MyApp.getInstance().getSystemService(Service.AUDIO_SERVICE);
        setAutoRegisterEventBus(true);

    }

    private boolean isVisible = false;

    PlayerView mPlayerView;

    @Override
    public void onPause() {
        super.onPause();
        if (mPlayerView == null) {
            return;
        }
        mPlayerView.setVolume(0);
//        if (mAudioManager != null) {
//            volume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
//            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_PLAY_SOUND);
//        }
        if (mPlayerView.canPause()) {
            lastPlayTime = mPlayerView.getCurrentPosition();
            mPlayerView.stopPlayback();
        } else {
            mPlayerView.pause();
        }
    }


    @Subscribe
    public void onEventNetworkChanged(EventNetworkChanged eventNetworkChanged) {
        if (eventNetworkChanged != null) {

        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
//        if (volume != null) {
////            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, AudioManager.FLAG_PLAY_SOUND);
//            mPlayerView.setVolume(1);
//        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        isVisible = isVisibleToUser;
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mPlayerView == null) {
            return;
        }
        mPlayerView.resumePlay(lastPlayTime);
//        if (volume != null) {
//            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, AudioManager.FLAG_PLAY_SOUND);
//        }
        mPlayerView.setVolume(1);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPlayerView == null) {
            return;
        }
        mPlayerView.stopPlayback();
    }

    @Override
    protected void initData() {

    }

//
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void onEventGprsNotificationConfigChange(EventGprsNotificationConfigChange eventBus){
//        if (eventBus.isOn) {
//            mPlayerView.pause();
//        }
//    }


    /**
     * 当前fragment是否可见
     *
     * @return
     */
    public boolean isFragmentVisible() {
        return isVisible;
    }


}